﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReceiptEdit.aspx.cs" Inherits="Main_Accounts_VoucherEntry_ReceiptEdit"
    Culture="en-GB" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxControl" %>
<%@ Register Assembly="CalendarExtenderPlus" Namespace="AjaxControlToolkitPlus" TagPrefix="advancedCalender" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Contra</title>
    <link href="../../../Styles/accounts.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript">
        function isNumberKey(control_id, evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 45 || charCode >= 47))
                return false;

            if (charCode == 46) {
                var dotcount = document.getElementById(control_id).value.split('.').length - 1;
                if (dotcount >= 1) {
                    return false;
                }
            }
            return true;
        }

        function RemoveZeroOnFocusCredit(ctl_id) {
            var totalCreditAmt = document.getElementById("txtTotalCredit").value;
            var totalDebitAmt = document.getElementById("txtTotalDebit").value;
            var txtCredit = document.getElementById(ctl_id);
            if (txtCredit.value == '0.00' && txtCredit.disabled != true) {
                if (parseFloat(totalDebitAmt) > parseFloat(totalCreditAmt)) {
                    txtCredit.value = parseFloat(parseFloat(totalDebitAmt) - parseFloat(totalCreditAmt)).toFixed(2)
                }
                else if (parseFloat(totalDebitAmt) == parseFloat(totalCreditAmt)) {
                    txtCredit.value = '';
                }
                else {
                    txtCredit.value = '';
                }
            }
        }
        function RemoveZeroOnFocusDebit(ctl_id) {
            var totalCreditAmt = document.getElementById("txtTotalCredit").value;
            var totalDebitAmt = document.getElementById("txtTotalDebit").value;
            var txtDebit = document.getElementById(ctl_id);
            if (txtDebit.value == '0.00' && txtDebit.disabled != true) {
                if (parseFloat(totalCreditAmt) > parseFloat(totalDebitAmt)) {
                    txtDebit.value = parseFloat(parseFloat(totalCreditAmt) - parseFloat(totalDebitAmt)).toFixed(2);
                }
                else if (parseFloat(totalCreditAmt) == parseFloat(totalDebitAmt)) {
                    txtDebit.value = '';
                }
                else {
                    txtDebit.value = '';
                }
            }
        }

        function SetZeroOnBlurDebit() {
            var totalDebitAmt = 0;
            var Count = document.getElementById("hidControl").value;
            for (var i = 1; i <= Count; i++) {
                var zero = 0;
                var txtDebit = document.getElementById("txtDebit" + i);
                if (txtDebit.value == '') {
                    if (txtDebit.disabled == false) {
                        txtDebit.value = zero.toFixed(2);
                        totalDebitAmt += zero;
                    }
                    else {
                        totalDebitAmt += zero;
                    }
                }
                else {
                    var debitAmt = parseFloat(txtDebit.value);
                    txtDebit.value = debitAmt.toFixed(2);
                    totalDebitAmt += debitAmt;
                }
            }
            document.getElementById("txtTotalDebit").value = totalDebitAmt.toFixed(2);
        }

        function SetZeroOnBlurCredit() {
            var totalCreditAmt = 0;
            var Count = document.getElementById("hidControl").value;
            for (var i = 1; i <= Count; i++) {

                var zero = 0;
                var txtCredit = document.getElementById("txtCredit" + i);
                if (txtCredit.value == '') {
                    if (txtCredit.disabled == false) {
                        txtCredit.value = zero.toFixed(2);
                        totalCreditAmt += zero;
                    }
                    else {
                        totalCreditAmt += zero;
                    }
                }
                else {
                    var creditAmt = parseFloat(txtCredit.value);
                    txtCredit.value = creditAmt.toFixed(2);
                    totalCreditAmt += creditAmt;
                }
            }
            document.getElementById("txtTotalCredit").value = totalCreditAmt.toFixed(2);
        }
        function validateAmt() {
            var flag = true;
            var Count = document.getElementById("hidControl").value;
            var totalDebitAmt = document.getElementById("txtTotalDebit").value;
            var totalCrditAmt = document.getElementById("txtTotalCredit").value;
            var txtVouNo = document.getElementById("txtVouNo").value;
            var txtDate = document.getElementById("txtDate").value;
            if (txtVouNo == '' || txtVouNo == '0') {
                alert('Specified Voucher No!');
                flag = false;
            }
            else if (txtDate == '') {
                alert('Specified Voucher Date!');
                flag = false;
            }
            else if (parseFloat(totalDebitAmt) > 0 || parseFloat(totalCrditAmt) > 0) {
                if (parseFloat(totalDebitAmt) != parseFloat(totalCrditAmt)) {
                    alert('Amount not maching !');
                    flag = false;
                }
            }
            else if (parseFloat(totalDebitAmt) == 0 || parseFloat(totalCrditAmt) == 0) {
                alert('Amount should not be zero');
                flag = false;
            }
            for (var i = 1; i <= Count; i++) {
                var ddlLedger = document.getElementById("ddlLedger" + i);
                if (ddlLedger.value == '-1') {
                    alert('Specified Ledger at row No. ' + i);
                    flag = false;
                    break;
                }
            }
            return flag;
        }

        function confirmDelete() {
            var Count = document.getElementById("hidControl").value;
            if (Count <= 2) {
                alert('Last Row Can not be Deleted!');
                return false;
            }
            else if (Count > 2) {
                var flag = confirm('Are you sure to Delete Last Row');
                if (flag == true) {
                    return true;
                }
                else {
                    return false;
                }
            }
        }
        function ValidateRepeatLedger(id) {
            var flag = true;
            var count = document.getElementById("hidControl").value;
            var currentLedger = document.getElementById(id);
            var lastNo = id.toString().substring(9);
            var currentValue = currentLedger.options[currentLedger.selectedIndex].value;
            for (var i = 1; i <= count; i++) {
                var previousLedger = document.getElementById("ddlLedger" + i);
                var previousValue = previousLedger.options[previousLedger.selectedIndex].value;
                if (currentLedger != previousLedger) {
                    if (currentValue == previousValue && previousValue != '-1' && currentValue != '-1') {
                        alert('Specifed Ledger can not be same');
                        currentLedger.selectedIndex = '0';
                        document.getElementById("txtBalance" + lastNo).value = '';
                        flag = false;
                        break;
                    }
                }
            }
            if (flag == true) {
                setTimeout('__doPostBack(\'' + id + '\',\'\')', 0);
            }
            return flag;
        }
        function ValidateNewRow() {
            var flag = true;
            var rowCount = document.getElementById("hidControl").value;
            var ddlDrCr = document.getElementById("ddlDrCr" + rowCount);
            var ddlLedger = document.getElementById("ddlLedger" + rowCount);
            var txtDebit = document.getElementById("txtDebit" + rowCount);
            var txtCredit = document.getElementById("txtCredit" + rowCount);
            if (ddlLedger.value == '-1') {
                alert('Specify Ledger First');
                flag = false;
                document.getElementById("ddlLedger" + rowCount).focus();
            }
            else if (ddlDrCr.value == '2' && (txtDebit.value == '0.00' || txtDebit.value == '')) {
                alert('Specify Debit Amount');
                flag = false;
                document.getElementById("txtDebit" + rowCount).focus();
            }
            else if (ddlDrCr.value == '1' && (txtCredit.value == '0.00' || txtCredit.value == '')) {
                alert('Specify Credit First');
                flag = false;
                document.getElementById("txtCredit" + rowCount).focus();
            }
            return flag;
        }

        function EnabledTabKey(id, e) {
            var keycode;
            if (window.event) {
                keycode = window.event.keyCode;
            }
            else if (e) {
                keycode = e.which;
            }
            else {
                return true;
            }

            if (keycode == 9) {
                // if is the tab key                
                return true;
            }
            else {
                return false;
            }
        }
        
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div style="margin-left: 5%; margin-right: 5%; width: 90%; text-align: center;">
        <asp:Label ID="lblMsg" runat="server" Font-Bold="true"></asp:Label>
    </div>
    <div style="margin-left: 5%; margin-right: 5%; width: 90%;">
        <asp:HiddenField ID="hidTranId" runat="server" />
        <asp:HiddenField ID="hidTranType" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <ContentTemplate>
                <table style="width: 100%; border: solid 1px #818181;">
                    <tr>
                        <td colspan="6" style="text-align: center; border-bottom: solid 1px #818181;" class="headtd">
                            Edit Receipt Voucher
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width: 15%">
                            Voucher No.
                        </td>
                        <td style="text-align: left; width: 35%">
                            <asp:TextBox ID="txtPrefix" runat="server" CssClass="input" Width="80px" Visible="false"
                                Enabled="false" ToolTip="Prefix"></asp:TextBox>
                            <asp:TextBox ID="txtVouNo" runat="server" CssClass="inputnumber" Width="80px" onkeydown="return EnabledTabKey(this.id, event);"
                                oncut="return false" ToolTip="Voucher Number"></asp:TextBox>
                            <asp:TextBox ID="txtSuffix" runat="server" CssClass="input" Width="80px" Visible="false"
                                Enabled="false" ToolTip="Suffix"></asp:TextBox>
                        </td>
                        <td style="text-align: left; width: 10%">
                            Date:
                        </td>
                        <td style="text-align: left; width: 40%">
                            <asp:TextBox ID="txtDate" runat="server" CssClass="input" Width="80px" onkeydown="return EnabledTabKey(this.id, event);"
                                oncut="return false" ToolTip="Voucher Date"></asp:TextBox>
                            <asp:ImageButton ID="imgDate" runat="server" ImageUrl="~/Images/calendar.gif" Enabled="false" />
                            <advancedCalender:CalendarExtenderPlus ID="Cal" runat="server" PopupButtonID="imgDate"
                                Format="dd/MM/yyyy" TargetControlID="txtDate" Enabled="false">
                            </advancedCalender:CalendarExtenderPlus>
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    <div style="margin-left: 5%; margin-right: 5%; margin-top: 10px; height: 300px; width: 90%;">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:HiddenField ID="hidControl" runat="server" Value="2" />
                <table width="100%" style="border: solid 1px #818181;">
                    <tr>
                        <td valign="top" style="height: 300px;">
                            <table id="tblVouEntry" runat="server" width="100%" cellpadding="2" cellspacing="0"
                                style="border: solid 1px #818181; margin-bottom: 15px">
                                <tr>
                                    <td style="text-align: center; width: 10%; border-bottom: solid 1px #818181; border-right: solid 1px #818181;"
                                        class="headtd">
                                        Dr/Cr
                                    </td>
                                    <td style="text-align: center; width: 30%; border-bottom: solid 1px #818181; border-right: solid 1px #818181;"
                                        class="headtd">
                                        Ledger
                                    </td>
                                    <td style="text-align: center; width: 20%; border-bottom: solid 1px #818181; border-right: solid 1px #818181;"
                                        class="headtd">
                                        Debit
                                    </td>
                                    <td style="text-align: center; width: 20%; border-bottom: solid 1px #818181; border-right: solid 1px #818181;"
                                        class="headtd">
                                        Credit
                                    </td>
                                    <td style="text-align: center; width: 20%; border-bottom: solid 1px #818181;" class="headtd">
                                        Ledger Balance
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:DropDownList ID="ddlDrCr1" runat="server" CssClass="dropdown" Width="98%" AutoPostBack="true"
                                            OnSelectedIndexChanged="ddlDrCr_SelectedIndexChanged" ToolTip="Debit/Credit">
                                            <asp:ListItem Text="Cr" Value="1"></asp:ListItem>
                                            <asp:ListItem Text="Dr" Value="2"></asp:ListItem>
                                        </asp:DropDownList>
                                        <asp:HiddenField ID="hidVouserial1" runat="server" Value="0" />
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="ddlLedger1" runat="server" CssClass="dropdown" ToolTip="Select Ledger for Transaction"
                                            AutoPostBack="true" OnSelectedIndexChanged="ddlLedger_OnSelectedIndexChanged"
                                            onchange="return ValidateRepeatLedger(this.id);" Width="98%">
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtDebit1" runat="server" MaxLength="21" CssClass="inputnumber"
                                            Width="98%" ToolTip="Debit Amount" onfocus="return RemoveZeroOnFocusDebit(this.id);"
                                            onblur="return SetZeroOnBlurDebit();" Enabled="false" onkeypress="return isNumberKey(this.id,event)"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtCredit1" MaxLength="21" runat="server" CssClass="inputnumber"
                                            Width="98%" ToolTip="Credit Amount" onfocus="return RemoveZeroOnFocusCredit(this.id);"
                                            onblur="return SetZeroOnBlurCredit();" Text="0.00" onkeypress="return isNumberKey(this.id,event)"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtBalance1" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Ledger Balance"
                                            Enabled="false"></asp:TextBox>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:DropDownList ID="ddlDrCr2" runat="server" CssClass="dropdown" Width="98%" AutoPostBack="true"
                                            OnSelectedIndexChanged="ddlDrCr_SelectedIndexChanged" ToolTip="Debit/Credit">
                                            <asp:ListItem Text="Cr" Value="1"></asp:ListItem>
                                            <asp:ListItem Text="Dr" Value="2"></asp:ListItem>
                                        </asp:DropDownList>
                                        <asp:HiddenField ID="hidVouserial2" runat="server" Value="0" />
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="ddlLedger2" runat="server" CssClass="dropdown" ToolTip="Select Ledger for Transaction"
                                            AutoPostBack="true" OnSelectedIndexChanged="ddlLedger_OnSelectedIndexChanged"
                                            onchange="return ValidateRepeatLedger(this.id);" Width="98%">
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtDebit2" MaxLength="21" runat="server" CssClass="inputnumber"
                                            Width="98%" ToolTip="Debit Amount" onfocus="return RemoveZeroOnFocusDebit(this.id);"
                                            onblur="return SetZeroOnBlurDebit();" Text="0.00" onkeypress="return isNumberKey(this.id,event)"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtCredit2" MaxLength="21" runat="server" CssClass="inputnumber"
                                            Width="98%" ToolTip="Credit Amount" onfocus="return RemoveZeroOnFocusCredit(this.id);"
                                            onblur="return SetZeroOnBlurCredit();" Enabled="false" onkeypress="return isNumberKey(this.id,event)"></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtBalance2" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Ledger Balance"
                                            Enabled="false"></asp:TextBox>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td style="border: solid 1px #818181; background-color: #f2f2f2">
                            <table width="100%" cellpadding="2" cellspacing="0">
                                <tr>
                                    <td style="text-align: right; width: 40%;">
                                        Total
                                    </td>
                                    <td style="text-align: center; width: 20%;">
                                        <asp:TextBox ID="txtTotalDebit" runat="server" CssClass="inputnumber" Width="98%"
                                            ToolTip="Total Debit Amount" Text="0.00" onkeydown="return EnabledTabKey(this.id, event);"></asp:TextBox>
                                    </td>
                                    <td style="text-align: center; width: 20%;">
                                        <asp:TextBox ID="txtTotalCredit" runat="server" CssClass="inputnumber" Width="98%"
                                            ToolTip="Total Credit Amount" Text="0.00" onkeydown="return EnabledTabKey(this.id, event);"></asp:TextBox>
                                    </td>
                                    <td style="text-align: center; width: 20%;">
                                        &nbsp;
                                    </td>
                                </tr>
                            </table>
                            <table width="100%" cellpadding="2" cellspacing="0">
                                <tr>
                                    <td style="text-align: right; width: 11%;">
                                        Cheque No.
                                    </td>
                                    <td style="text-align: left; width: 18%;">
                                        <asp:TextBox ID="txtChequeNo" MaxLength="6" runat="server" ToolTip="Cheque Number"
                                            CssClass="input" onkeypress="return isNumberKey(this.id,event)" Width="98%"></asp:TextBox>
                                    </td>
                                    <td style="text-align: right; width: 11%;">
                                        Narration
                                    </td>
                                    <td style="text-align: left; width: 60%;">
                                        <asp:TextBox ID="txtNarration" MaxLength="250" runat="server" ToolTip="Narration"
                                            CssClass="input" Width="98%"></asp:TextBox>
                                    </td>
                                </tr>
                            </table>
                            <div style="margin-left: 5%; margin-right: 5%; margin-top: 5px; width: 90%; margin-bottom: 5px">
                                <asp:Button ID="btnAddRow" runat="server" ToolTip="Add New Row!" Text="Add Row" CssClass="button"
                                    OnClick="btnAddRow_Click" OnClientClick="return ValidateNewRow();" />&nbsp;
                                <asp:Button ID="btnDeleteRow" runat="server" ToolTip="Delete Last Row!" Text="Delete Row"
                                    CssClass="button1" OnClientClick="return confirmDelete();" OnClick="btnDeleteRow_Click" />&nbsp;
                                <asp:Button ID="btnSave" runat="server" Text="Save" ToolTip="Save Reciept Transaction!"
                                    CssClass="button" OnClientClick="return validateAmt();" OnClick="btnSave_Click" />&nbsp;
                                <asp:Button ID="btnClose" runat="server" Text="Close" ToolTip="Close Current Page!"
                                    CssClass="button" OnClick="btnClose_Click" />&nbsp;
                            </div>
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
